import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const token = ref(localStorage.getItem('token') || '')
  const userRole = ref(localStorage.getItem('userRole') || '')
  const userName = ref(localStorage.getItem('userName') || '')
  const userAvatar = ref(localStorage.getItem('userAvatar') || '')

  const setToken = (newToken) => {
    token.value = newToken
    localStorage.setItem('token', newToken)
  }

  const setUserRole = (role) => {
    userRole.value = role
    localStorage.setItem('userRole', role)
  }

  const setUserName = (name) => {
    userName.value = name
    localStorage.setItem('userName', name)
  }

  const setUserAvatar = (avatar) => {
    userAvatar.value = avatar
    localStorage.setItem('userAvatar', avatar)
  }

  const clearUserInfo = () => {
    token.value = ''
    userRole.value = ''
    userName.value = ''
    userAvatar.value = ''
    localStorage.clear()
  }

  return {
    token,
    userRole,
    userName,
    userAvatar,
    setToken,
    setUserRole,
    setUserName,
    setUserAvatar,
    clearUserInfo
  }
}, {
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: localStorage,
        paths: ['token', 'userRole', 'userName', 'userAvatar']
      }
    ]
  }
}) 